﻿using System;
using System.Collections.Generic;

using MiP.Common;
using MiP.XmlCmd.Shell.Args;
using MiP.XmlCmd.Shell.Commands;

namespace MiP.XmlCmd.Shell
{
    public class ProgramRunner : IProgramRunner
    {
        private readonly TransformCommand _command;
        private readonly IArgsParser _parser;
        private readonly ILogger _logger;


        public ProgramRunner(TransformCommand command, IArgsParser parser, ILogger logger)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            if (parser == null)
                throw new ArgumentNullException("parser");
            if (logger == null)
                throw new ArgumentNullException("logger");

            _command = command;
            _parser = parser;
            _logger = logger;
        }

        public void Run(IEnumerable<string> args)
        {
            Arguments arguments = _parser.Parse(args);
            if (arguments == null) // help or parsing error
                return;

            _logger.LogLevel = arguments.LogLevel;

            _command.SetArguments(arguments);
            _command.Execute();
        }
    }
}